<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>12 购物车</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.min.js"></script>
    <style>
        .table {
            width: 1000px;
            margin: 50px auto;
        }

        .table .tr {
            width: 1000px;
        }

        .table .tr .item {
            width: 150px;
            height: 50px;
            line-height: 50px;
            background-color:thistle;
            color: coral;
            text-align: center;
            float: left;
        }
        .table .tr .item input{
            width: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="table">
            <div class="tr">
                <div class="item">
                    全选
                    <input type="checkbox" :checked="checkAll" @change="setAllcheck">
                </div>
                <div class="item">
                    商品名称
                </div>
                <div class="item">
                    商品价格
                </div>
                <div class="item">
                    商品数量
                </div>
                <div class="item">
                    小计
                </div>
                <div class="item">
                    操作
                </div>
            </div>
            <div class="tr" v-for="item in list">
                <div class="item">
                    <input type="checkbox" v-model="item.ischecked">
                </div>
                <div class="item">
                    {{item.goodsname}}
                </div>
                <div class="item">
                    {{item.goodsprice.toFixed(2)}}
                </div>
                <div class="item">
                    <button v-show="item.buynum > 1" @click="cale(item,-1)">-</button>
                    <input type="number" :value="item.buynum">
                    <button @click="cale(item,1)">+</button>
                </div>
                <div class="item">
                    {{(item.buynum * item.goodsprice).toFixed(2)}}
                </div>
                <div class="item" @click="del(item.id)">
                    删除
                </div>
            </div>
            <div class="tr">
                <div class="item" @click="Alldel">
                    全删
                </div>
                <div class="item">
                    
                </div>
                <div class="item">
                    
                </div>
                <div class="item">
                    总数{{numtotal}}
                </div>
                <div class="item">
                    总价
                    {{pricetotal.toFixed(2)}}
                </div>
                <div class="item">
                    
                </div>
            </div>
        </div>
    </div>   
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            list: [ ],
            checkAll:false,
        },
        computed:{
            numtotal(){
                let selectList = this.list.filter(item => item.ischecked == true);
                if(selectList.length){
                    return selectList.map(item =>item.buynum).reduce((a,b) => a+b);
                }else{
                    return 0;
                }
            },
            pricetotal(){
                let selectList = this.list.filter(item => item.ischecked == true);
                if(selectList.length){
                    return selectList.map(item => item.buynum*item.goodsprice).reduce((a,b) => a+b); 
                }else{
                    return 0.00;
                }
            }
        },
        async created(){
            let userinfo = window.localStorage.getItem("userinfo");
            userinfo = JSON.parse(userinfo);
            let userid = userinfo;
            if(userid){     //有登录的用户
                let {data:list} = await axios({
                    method:"get",
                    url:"http://localhost:2345/shoppingcar",
                    param:{
                        userid,
                    }
                })
                this.list = list;


            }else{            //没有登录的用户
                location.href = "./04 登录.html?returnUrl=" + encodeURIComponent(location.href);
            }
        },
        methods: {
            cale(item,num){
                item.goodsnum = item.goodsnum+num;
            },
            setAllcheck(){
                let flag = window.event.target.checked;
                this.list.forEach(item => item.ischecked = flag);
            },
            del(id){

                




                this.list = this.list.filter(item => item.id != id);
            },
            Alldel(){
                this.list = this.list.filter(item => item.ischecked == false);
            },

        },
        watch:{
            list:{
                deep:true,
                handler(newlist){

                    if(newlist.length){
                        this.checkAll = newlist.every(item => item.ischecked)

                    }else{
                        this.checkAll = false;
                    }
                }
            }
        }
    })
</script>

</html>